// Copyright (c) 2008 Matthew Johnson
// Licensed under the MIT License (see license.txt)
package actionml.animation {
	import flash.events.EventDispatcher;
	internal class RendererBase extends EventDispatcher implements IRendererInternal {
		public function RendererBase() {
			if (this["constructor"] == RendererBase) {
				throw new Error("RendererBase is an abstract class.");
			}
		}
		public function get name():String {
			throw new Error("Renderer.getName not implemented.");
		}
		public function get rendering():Boolean {
			throw new Error("Renderer.rendering not implemented. ");
		}
		
		protected var _target:Object;
		public function get target():Object {
			return _target;
		}
		public function setTarget(value:Object) {
			_target = value;
		}
		public function interrupted(interruptedBy:IRenderer) {
			this.sendEvent(new RenderEvent(RenderEvent.INTERRUPT_RENDERING, this, interruptedBy));
			IRendererInternal(interruptedBy).interrupt(this);
		}
		public function interrupt(interrupted:IRenderer) {
			// TODO: override in property tween renderer to detect if heading in same direction and then 
			// strip easeIn out of the easing function to use for this rendering
		}
		public function prepareToRender() {
			this.sendEvent(new RenderEvent(RenderEvent.BEGIN_RENDERING, this));
		}
		public function render(rc:RenderContext) {
			
		}		
		public function stopRendering() {
			this.sendEvent(new RenderEvent(RenderEvent.STOP_RENDERING, this));
		}
		protected function sendEvent(e:RenderEvent) {
			this.dispatchEvent(e);
			if (_target is EventDispatcher) {
				_target.dispatchEvent(e);
				_target.dispatchEvent(new RenderEvent(e.type + this.name, this));
			}
			var generalHandler:String = e.type + "Handler";
			if (generalHandler in _target && _target[generalHandler] is Function) {
				_target[generalHandler](this);
			}
			var specificHandler:String = e.type + this.name + "Handler";
			if (specificHandler in _target && _target[specificHandler] is Function) {
				_target[specificHandler](this);
			}
		}
	}
}
